
Ext.define('cloud.controller.Coupon', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    config: {
        refs: {
            coupon: 'coupon',
            couponruleadd:'couponruleadd',
            coupon_add:'coupon #coupon_add',
            conpon_rule_submit:'couponruleadd #conpon_rule_submit',
            user_status:'userDetail #user_status',
            search_user:'user #search_user'

        },
        control: {
            coupon_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: "loadmask"});
                    var couponadd=this.getCouponruleadd()||Ext.create('cloud.view.coupon.CouponRuleAdd');
                    this.getCoupon().push(couponadd);
                    Ext.Viewport.unmask();
                }
            },
            "coupon list":{
                itemtap: function (list, item, index, record, target, event) {
                    if (target.target.name == 'toupon_detele'){
                        Ext.Msg.confirm('确认删除？', '', function (data) {
                            if (data == 'yes') {
                                Ext.Viewport.mask({xtype: 'loadmask' });
                                Ext.Ajax.request({
                                    url: GLOBAL.api_url + 'coupon_rule_delete',
                                    params: {
                                        typeid: record.get('id')
                                    },
                                    scope: this,
                                    success: function (response) {
                                        Ext.Viewport.unmask();
                                        Ext.getStore('Coupon').load({params:{cadid:localStorage.getItem('cadid')}});
                                    },
                                    failure: function () {
                                        Ext.Viewport.unmask();
                                        Ext.Msg.alert('服务器维护中...')
                                        return false;
                                    }
                                })
                            }
                            Ext.Viewport.unmask();
                        }, this);
                    }
                }
            },
            conpon_rule_submit:{
                tap:function(){
                    var name = Ext.getCmp('rule').getValue();
                    if (Ext.isEmpty(name)) {
                        Ext.Msg.alert('请输入规则描述');
                        return
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getCouponruleadd().submit({
                        url: GLOBAL.api_url + 'coupon_rule_add&cadid=' + localStorage.getItem('cadid'),
                        scope: this,
                        method: 'POST',
                        success: function (from, result){
                            Ext.Viewport.unmask();
                            if (result.Variables.Result.code == 0) {
                                this.getCoupon().pop()
                            }
                            Ext.getStore('Coupon').load({params: {cadid: localStorage.getItem('cadid')}});
                        },
                        failure:function(){
                            Ext.Viewport.unmask();
                            Ext.Msg.alert('请求失败', '服务器维护中....');
                        }
                    })
                }
            }
        }
    }
});